// DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Demo.h"
#include "DemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog

CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDemoDlg)
	m_Used = _T("");
	m_SN = _T("");
	m_COUNT = _T("");
	m_Regname = _T("");
	m_Hint = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	s_zkfp = _T("");
	sRegTemplate = _T("");
}

void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemoDlg)
	DDX_Control(pDX, IDC_ZKFPENGX2, m_zkfp);
	DDX_Text(pDX, IDC_EDUsed, m_Used);
	DDX_Text(pDX, IDC_EDSN, m_SN);
	DDX_Text(pDX, IDC_EDCount, m_COUNT);
	DDX_Text(pDX, IDC_EDReg, m_Regname);
	DDX_Text(pDX, IDC_EDHint, m_Hint);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CDemoDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTNInit, OnBTNInit)
	ON_BN_CLICKED(IDC_BTNSave, OnBTNSave)
	ON_BN_CLICKED(IDC_BTNReg, OnBTNReg)
	ON_BN_CLICKED(IDC_BTNIdentify, OnBTNIdentify)
	ON_BN_CLICKED(IDC_BTNVer, OnBTNVer)
	ON_WM_DESTROY()
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers

BOOL CDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	FingerCount = 0;
    fpcHandle = m_zkfp.CreateFPCacheDB();
	VariantInit(&FRegTemplate);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDemoDlg::OnBTNInit() 
{
	char buffer[10];

	CheckRadioButton(IDC_RADIOBMP, IDC_RADIOJPG, IDC_RADIOBMP);
	// TODO: Add your control notification handler code here
	if (m_zkfp.InitEngine() == 0){
		m_SN = m_zkfp.GetSensorSN();
		ltoa(m_zkfp.GetSensorIndex(), buffer, 10);
		m_Used = buffer;
		ltoa(m_zkfp.GetSensorCount(), buffer, 10);
		m_COUNT = buffer;
		SetDlgItemText(IDC_EDUsed, m_Used);
		SetDlgItemText(IDC_EDCount, m_COUNT);
		SetDlgItemText(IDC_EDSN, m_SN);		
		MessageBox("Initial Succeed");
	} else
		MessageBox("Initial Failed");
	FMatchType = 0;
}

BEGIN_EVENTSINK_MAP(CDemoDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CDemoDlg)
	ON_EVENT(CDemoDlg, IDC_ZKFPENGX2, 8 /* OnImageReceived */, OnOnImageReceivedZkfpengx2, VTS_PBOOL)
	ON_EVENT(CDemoDlg, IDC_ZKFPENGX2, 10 /* OnCapture */, OnOnCaptureZkfpengx2, VTS_BOOL VTS_VARIANT)
	ON_EVENT(CDemoDlg, IDC_ZKFPENGX2, 9 /* OnEnroll */, OnOnEnrollZkfpengx2, VTS_BOOL VTS_VARIANT)
	ON_EVENT(CDemoDlg, IDC_ZKFPENGX2, 5 /* OnFeatureInfo */, OnOnFeatureInfoZkfpengx2, VTS_I4)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CDemoDlg::OnOnImageReceivedZkfpengx2(BOOL FAR* AImageValid) 
{
	// TODO: Add your control notification handler code here
	HDC hdc;

	hdc = this->GetDC()->m_hDC;

	m_zkfp.PrintImageAt(long(hdc), 330, 2, m_zkfp.GetImageWidth(), m_zkfp.GetImageHeight());
}

void CDemoDlg::OnBTNSave() 
{
	// TODO: Add your control notification handler code here
	if (IsDlgButtonChecked(IDC_RADIOBMP) != 0)
		m_zkfp.SaveBitmap("c:\\fingerprint.bmp");
	else
		m_zkfp.SaveJPG("c:\fingerprint.jpg");
	MessageBox("Fingerprint Image saved C:\\FingerPrint");
}

void CDemoDlg::OnBTNReg() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if (m_Regname.Compare("") == 0){
		MessageBox("Please enter PIN");
		return;
	}
	m_zkfp.BeginEnroll();
	SetDlgItemText(IDC_EDHint, "start register");		
}

void CDemoDlg::OnBTNIdentify() 
{
	// TODO: Add your control notification handler code here
    if (m_zkfp.GetIsRegister())
        m_zkfp.CancelEnroll();
	SetDlgItemText(IDC_EDHint, "Identify(1:N)");		
    FMatchType = 2;	
}

void CDemoDlg::OnBTNVer() 
{
	// TODO: Add your control notification handler code here
    if (m_zkfp.GetIsRegister())
        m_zkfp.CancelEnroll();
	SetDlgItemText(IDC_EDHint, "Verify(1:1)");		
    FMatchType = 1;		
}

BOOL CDemoDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
    m_zkfp.FreeFPCacheDB(fpcHandle);	
	return CDialog::DestroyWindow();
}

void CDemoDlg::OnOnCaptureZkfpengx2(BOOL ActionResult, const VARIANT FAR& ATemplate) 
{
	// TODO: Add your control notification handler code here
  long fi;
  long Score, ProcessNum;
  BOOL RegChanged;
  char buffer[80];
  CString sTemp;
  BSTR bTemp;

  sTemp = m_zkfp.GetTemplateAsString();
  SetDlgItemText(IDC_EDHint, "Acquired Fingerprint template");		
  if (FMatchType == 1){
	 bTemp = sRegTemplate.AllocSysString();
     //if (m_zkfp.VerFinger(&FRegTemplate, ATemplate, FALSE, &RegChanged))
     if (m_zkfp.VerFingerFromStr(&bTemp, (LPCTSTR)sTemp, FALSE, &RegChanged))
        MessageBox("Verify Succeed");
     else
        MessageBox("Verify Failed");

  }	else if (FMatchType == 2){  //1:N
		Score = 8;
		//fi = m_zkfp.IdentificationInFPCacheDB(fpcHandle, ATemplate, &Score, &ProcessNum);
		fi = m_zkfp.IdentificationFromStrInFPCacheDB(fpcHandle, (LPCTSTR)sTemp, &Score, &ProcessNum);
	    if (fi == -1)
	       MessageBox("Identify Failed");
		else{
		   sprintf(buffer, "Identify Failed Name = %s Score = %d  Processed Number = %d", FFingerNames[fi], Score, ProcessNum);
    	   MessageBox(buffer);
		}
	}   
}

void CDemoDlg::OnOnEnrollZkfpengx2(BOOL ActionResult, const VARIANT FAR& ATemplate) 
{
	// TODO: Add your control notification handler code here
  VARIANT pTemplate;
  
  if (!ActionResult)
    MessageBox("Register Failed");
  else{
    MessageBox("Register Succeed");

	VariantClear(&FRegTemplate);
	pTemplate = m_zkfp.GetTemplate();

	sRegTemplate = m_zkfp.GetTemplateAsString();

	s_zkfp = m_zkfp.EncodeTemplate1(pTemplate);

    VariantCopy(&FRegTemplate, &pTemplate);
    
    m_zkfp.SaveTemplate("c:\\fingerprint.tpl", ATemplate);
    
	m_zkfp.AddRegTemplateStrToFPCacheDB(fpcHandle, FingerCount, (LPCTSTR)sRegTemplate);

	UpdateData(TRUE);
	strcpy(FFingerNames[FingerCount], m_Regname);
    FingerCount = FingerCount + 1;
  }
}

void CDemoDlg::OnOnFeatureInfoZkfpengx2(long AQuality) 
{
	// TODO: Add your control notification handler code here
  CString sTemp;
  char buffer[10];

  sTemp = "";
  if (m_zkfp.GetIsRegister()){
	 ltoa(m_zkfp.GetEnrollIndex(), buffer, 10);
     sTemp = "Register Status: still press finger ";
	 sTemp = sTemp + buffer + " times";
  }
  sTemp = sTemp + " Fingerprint quality";
  ltoa(AQuality, buffer, 10);
  if (AQuality != 0)
     sTemp = sTemp + " not good "  + buffer;
  else
     sTemp = sTemp +  " good";
  SetDlgItemText(IDC_EDHint, sTemp);
}

void CDemoDlg::OnDestroy() 
{
	CDialog::OnDestroy();
}

void CDemoDlg::OnClose() 
{
m_zkfp.EndEngine();	
	CDialog::OnClose();
}
